﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AMS.LCM.Models
{
    public class LCMModel
    {
        #region Events
        public event EventHandler<LCMEventArgs> LCMCalculated;
        private void RaiseLCMCalculated(LCMEventArgs args)
        {
            EventHandler<LCMEventArgs> eh = LCMCalculated;
            if (eh != null)
                eh(this, args);
        } 
        #endregion

        public LCMModel()
        { }

        public long CalculateLCM(int max)
        {
            return CalculateLCM(2, max);
        }

        public long CalculateLCM(int min, int max)
        {
            if (min < 2 || max < 2)
                throw new ArgumentOutOfRangeException("min and max must me greater than 2");

            if (min > max)
            {
                int h = max;
                max = min;
                min = h;
            }

            PrimeFactors total = new PrimeFactors();
            for (int i = min; i <= max; i++)
            {
                PrimeFactors pf = new PrimeFactors(i);
                total.Combine(pf);
            }

            long result = total.Value;
            RaiseLCMCalculated(new LCMEventArgs(result));

            return total.Value;
        }
    }
}
